package com.example.xmpic.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.example.common.base.BasePresentActivity;
import com.example.xmpic.R;
import com.example.xmpic.databinding.ActivityDeviceCameraBinding;
import com.example.common.utils.MediaPlayerUtil;
import com.example.xmpic.mvp.contract.CameraDeviceContract;
import com.example.xmpic.mvp.present.CameraDevicePresenter;
import com.example.xmpic.support.FunError;
import com.example.xmpic.support.FunPath;
import com.example.xmpic.support.FunSupport;
import com.example.xmpic.support.OnFunDeviceListener;
import com.example.xmpic.support.OnFunDeviceOptListener;
import com.example.xmpic.support.config.AVEncVideoWidget;
import com.example.xmpic.support.config.OPTimeQuery;
import com.example.xmpic.support.config.SystemInfo;
import com.example.xmpic.support.models.FunDevice;
import com.example.xmpic.support.models.FunLoginType;
import com.example.xmpic.support.models.FunStreamType;
import com.example.xmpic.support.utils.UIFactory;
import com.example.common.utils.PermissionUtil;
import com.example.xmpic.ui.data.CameraBean;
import com.example.xmpic.ui.utils.CameraDaoUtil;
import com.example.xmpic.ui.utils.LocalFileManager;
import com.lib.EPTZCMD;
import com.lib.sdk.struct.H264_DVR_FILE_DATA;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author skygge
 * @Date on 2020-02-19.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：摄像头界面
 */
public class CameraDeviceActivity extends BasePresentActivity<CameraDevicePresenter, ActivityDeviceCameraBinding> implements CameraDeviceContract.IView,
		OnFunDeviceOptListener, OnPreparedListener, OnErrorListener, OnInfoListener, OnFunDeviceListener{

	private FunDevice mFunDevice = null;

    private final int MESSAGE_AUTO_HIDE_DIRECTION  = 0x107;

	private boolean mCanToPlay = false;

	private int nPTZCommand = -1;
	private enum ActionType{ACTION_LEFT, ACTION_RIGHT, ACTION_DOWN, ACTION_UP}

	private String deviceId;
	private CameraBean mCameraBean;
	private MediaPlayerUtil mMediaPlayer;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_device_camera;
	}

	@Override
	protected void initialize() {
		super.initialize();
		String mPermission = Manifest.permission.RECORD_AUDIO;
		PermissionUtil.requestPermission(this, new String[]{mPermission});
		deviceId = getIntent().getStringExtra("FUN_DEVICE_ID");
		if(TextUtils.isEmpty(deviceId)) finish();
	}

	@Override
	protected void initPresent() {
		super.initPresent();
		mPresent = new CameraDevicePresenter(this);
	}

	@Override
	protected void initView() {
		super.initView();
		LocalFileManager.getInstance().scanFile(mContext, FunPath.DEFAULT_PATH);
        // 注册设备操作回调
        FunSupport.getInstance().registerOnFunDeviceOptListener(this);
        // 注册设备操作回调
        FunSupport.getInstance().registerOnFunDeviceListener(this);
		// 设置登录方式为本地登录
		FunSupport.getInstance().setLoginType(FunLoginType.LOGIN_BY_LOCAL);
		showAsPortrait();
	}

	@Override
	protected void initData() {
		super.initData();
		mPresent.requestDeviceStatus(deviceId);
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	protected void initListener() {
		super.initListener();
		mDBind.ivCameraBack.setOnClickListener(v -> onBackPressed());
		mDBind.ivCameraSetting.setOnClickListener(v -> mPresent.gotoCameraSetting());
		mDBind.btnStream.setOnClickListener(v -> mPresent.switchStream());
		mDBind.btnCapture.setOnClickListener(v -> tryToCapture());
		mDBind.btnVoice.setOnClickListener(v -> mPresent.switchMute(mDBind.funVideoView.getStreamType()));
		mDBind.btnScreenRatio.setOnClickListener(v -> mPresent.switchOrientation());
		mDBind.btnDevCapture.setOnClickListener(v -> mPresent.startPictureList());
		mDBind.btnDevRecord.setOnClickListener(v -> mPresent.startRecordList(mCameraBean.getDeviceName()));

		mDBind.layoutPlayWnd.setOnTouchListener(new OnVideoViewTouchListener());
		mDBind.btnVoiceTalk.setOnClickListener(v -> {});
		mDBind.btnVoiceTalk.setOnTouchListener(mIntercomTouchLs);

		mDBind.funVideoView.setOnPreparedListener(this);
		mDBind.funVideoView.setOnErrorListener(this);
		mDBind.funVideoView.setOnInfoListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mCameraBean = CameraDaoUtil.getInstance().findCameraByCameraId(deviceId);
		if (mCameraBean == null){
			finish();
		}else {
			String cameraName = mCameraBean.getDeviceName();
			mDBind.tvCameraTitle.setText(TextUtils.isEmpty(cameraName) ? getString(R.string.dev_type_monitor) : cameraName);
		}
		mPresent.checkNetWork(mCanToPlay);
	}

	@Override
	protected void onPause() {
		mPresent.stopTalk();
		stopMedia();
		super.onPause();
	}

	@Override
	public void onConfigurationChanged(@NonNull Configuration newConfig) {
		// 检测屏幕的方向：纵向或横向
	    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// 当前为横屏， 在此处添加额外的处理代码
			showAsLandscape();
	    }else if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
			// 当前为竖屏， 在此处添加额外的处理代码
			showAsPortrait();
		}
		super.onConfigurationChanged(newConfig);
	}

	/**
	 * 视频截图,并延时一会提示截图对话框
	 */
	private void tryToCapture() {
		if (!mDBind.funVideoView.isPlaying()) {
			showToast(getString(R.string.media_capture_failure_need_playing));
			return;
		}
		mMediaPlayer = new MediaPlayerUtil(mContext);
		mMediaPlayer.playPhotograph();
		String path = mDBind.funVideoView.captureImage(mFunDevice.getDevSn());	//图片异步保存
		if (!TextUtils.isEmpty(path)) {
			mPresent.screenshot(path);
		}
	}

	private void showAsLandscape() {
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mDBind.layoutFunctionControl.setVisibility(View.GONE);
		mDBind.btnScreenRatio.setImageResource(R.mipmap.icon_full_back);
		// 视频窗口全屏显示
		LinearLayout.LayoutParams mParams = (LinearLayout.LayoutParams) mDBind.layoutVideoPlay.getLayoutParams();
		mParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
		mDBind.layoutVideoPlay.setLayoutParams(mParams);
		RelativeLayout.LayoutParams mVideoParams = (RelativeLayout.LayoutParams) mDBind.funVideoView.getLayoutParams();
		mVideoParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
		mDBind.funVideoView.setLayoutParams(mVideoParams);
		// 上面标题半透明背景
		mDBind.layoutCameraTop.setBackgroundColor(0x40000000);
		mDBind.tvCameraTitle.setTextColor(ContextCompat.getColor(mContext, R.color.white));
	}

	private void showAsPortrait() {
		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// 还原上面标题栏背景
		mDBind.layoutCameraTop.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
		mDBind.layoutCameraTop.setVisibility(View.VISIBLE);
		// 视频显示为小窗口
		LinearLayout.LayoutParams mParams = (LinearLayout.LayoutParams) mDBind.layoutVideoPlay.getLayoutParams();
		mParams.height = (int)getResources().getDimension(com.example.xmpic.R.dimen.margin_400);
		mDBind.layoutVideoPlay.setLayoutParams(mParams);
		RelativeLayout.LayoutParams mVideoParams = (RelativeLayout.LayoutParams) mDBind.funVideoView.getLayoutParams();
		mVideoParams.height = (int)getResources().getDimension(com.example.xmpic.R.dimen.margin_240);
		mDBind.funVideoView.setLayoutParams(mVideoParams);
		// 显示底部的控制按钮区域
		mDBind.layoutVideoControl.setVisibility(View.VISIBLE);
		mDBind.layoutFunctionControl.setVisibility(View.VISIBLE);
		mDBind.btnScreenRatio.setImageResource(R.mipmap.icon_full);
		mDBind.tvCameraTitle.setTextColor(ContextCompat.getColor(mContext, R.color.black));
	}

	private void refreshSystemInfo() {
		if (null != mFunDevice ) {
			OPTimeQuery mQuery = (OPTimeQuery) mFunDevice.getConfig(OPTimeQuery.CONFIG_NAME);
			if (null != mQuery) {
				String mOPTimeQuery = mQuery.getOPTimeQuery();
				mDBind.videoTime.setText(mOPTimeQuery);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
				try {
					Date date = sdf.parse(mOPTimeQuery);
					mDBind.videoTime.setDevSysTime(date.getTime());
					mDBind.videoTime.onStartTimer();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void onDeviceListChanged() {

	}

	@Override
	public void onDeviceStatusChanged(FunDevice funDevice) {
	    this.mFunDevice = funDevice;
	    mPresent.setFunDevice(funDevice);
		FunSupport.getInstance().requestDeviceCmdGeneral(mFunDevice, new OPTimeQuery());
		FunSupport.getInstance().requestDeviceConfig(mFunDevice, AVEncVideoWidget.CONFIG_NAME, mFunDevice.CurrChannel);
	}

	@Override
	public void onDeviceAddedSuccess() {

	}

	@Override
	public void onDeviceAddedFailed(Integer errCode) {

	}

	@Override
	public void onDeviceRemovedSuccess() {

	}

	@Override
	public void onDeviceRemovedFailed(Integer errCode) {

	}

	@Override
	public void onAPDeviceListChanged() {

	}

	@Override
	public void onLanDeviceListChanged() {

	}

	@Override
	public void onDeviceLoginSuccess(final FunDevice funDevice) {
		dismissProgressDialog();
		if (null != mFunDevice && null != funDevice) {
			if (mFunDevice.getId() == funDevice.getId()) {
				mPresent.requestSystemInfo();
			}
		}
	}

	@Override
	public void onDeviceLoginFailed(final FunDevice funDevice, final Integer errCode) {
		onLoginFailed();
		// 如果账号密码不正确,那么需要提示用户,输入密码重新登录
		if (errCode == FunError.EE_DVR_PASSWORD_NOT_VALID) {
			mPresent.showInputPasswordDialog();
		}
	}

	@Override
	public void onDeviceGetConfigSuccess(FunDevice funDevice, String configName, int nSeq) {
		if (SystemInfo.CONFIG_NAME.equals(configName)) {
			//此处为示例如何取通道信息，可能会增加打开视频的时间，可根据需求自行修改代码逻辑
			if (funDevice.channel == null) {
				FunSupport.getInstance().requestGetDevChnName(funDevice);
				mPresent.requestSystemInfo();
				return;
			}
			onLoginFailed();
			// 设置允许播放标志
			mCanToPlay = true;
			mPresent.checkNetWork(true);
		} else {
			mPresent.onConfigSuccess(mDBind.funVideoView.getStreamType(), configName);
		}

		if(OPTimeQuery.CONFIG_NAME.equals(configName)){
			refreshSystemInfo();
		}
		AVEncVideoWidget avEnc = (AVEncVideoWidget) mFunDevice.getConfig(AVEncVideoWidget.CONFIG_NAME);
		if (null != avEnc) {
			mDBind.videoTime.setVisibility(avEnc.timeTitleAttribute.EncodeBlend?View.GONE:View.VISIBLE);
		}
	}

	@Override
	public void onDeviceGetConfigFailed(final FunDevice funDevice, final Integer errCode) {
	}

	@Override
	public void onDeviceSetConfigSuccess(final FunDevice funDevice, final String configName) {
		mPresent.onConfigSuccess(mDBind.funVideoView.getStreamType(), configName);
	}

	@Override
	public void onDeviceSetConfigFailed(final FunDevice funDevice,
			final String configName, final Integer errCode) {
	}

	@Override
	public void onDeviceChangeInfoSuccess(final FunDevice funDevice) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDeviceChangeInfoFailed(final FunDevice funDevice, final Integer errCode) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDeviceOptionSuccess(final FunDevice funDevice, final String option) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDeviceOptionFailed(final FunDevice funDevice, final String option, final Integer errCode) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDeviceFileListChanged(FunDevice funDevice) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDeviceFileListChanged(FunDevice funDevice, H264_DVR_FILE_DATA[] datas) {
	}

	@Override
	public void onPrepared(MediaPlayer arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		showToast(getString(R.string.media_play_error));
		if ( FunError.EE_TPS_NOT_SUP_MAIN == extra || FunError.EE_DSS_NOT_SUP_MAIN == extra ) {
			// 不支持高清码流,设置为标清码流重新播放
			mDBind.funVideoView.setStreamType(FunStreamType.STREAM_SECONDARY);
			playRealMedia();
		}
		return true;
	}

	@Override
	public boolean onInfo(MediaPlayer arg0, int what, int extra) {
		Log.i(TAG,"onInfo:what="+what);
		dismissProgressDialog();
		if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
			mDBind.textVideoStat.setText(R.string.media_player_buffering);
			mDBind.textVideoStat.setVisibility(View.VISIBLE);
		} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
			mDBind.textVideoStat.setVisibility(View.GONE);
		}
		return true;
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		mFunDevice.CurrChannel = arg1;
		if (mCanToPlay) {
			playRealMedia();
		}
	}

	@Override
	public void onDeviceFileListGetFailed(FunDevice funDevice) {
		// TODO Auto-generated method stub
	}

	@Override
	public void startOpenCamera() {
		showProgressDialog();
		mDBind.textVideoStat.setText(R.string.opening);
		mDBind.textVideoStat.setVisibility(View.VISIBLE);
	}

	@Override
	public void showLoginView() {
		mDBind.textVideoStat.setText(R.string.opening);
		mDBind.textVideoStat.setVisibility(View.VISIBLE);
	}

	@Override
	public void showDeviceOffLine() {
		dismissProgressDialog();
		showToast(getString(R.string.device_stauts_offline));
		mDBind.textVideoStat.setText(getString(R.string.device_stauts_offline));
		mDBind.textVideoStat.setVisibility(View.VISIBLE);
	}


	@Override
	public void playRealMedia() {
		if (mFunDevice.isRemote) {
			mDBind.funVideoView.setRealDevice(mFunDevice.getDevSn(), mFunDevice.CurrChannel);
		} else {
			String deviceIp = FunSupport.getInstance().getDeviceWifiManager().getGatewayIp();
			mDBind.funVideoView.setRealDevice(deviceIp, mFunDevice.CurrChannel);
		}
		// 打开声音
		mDBind.funVideoView.setMediaSound(true);

		// 设置当前播放的码流类型
		if (FunStreamType.STREAM_SECONDARY == mDBind.funVideoView.getStreamType()) {
			mDBind.btnStream.setText(R.string.stream_hd);
		} else {
			mDBind.btnStream.setText(R.string.stream_sd);
		}
	}

	@Override
	public void setMediaSound(boolean play) {
		mDBind.funVideoView.setMediaSound(play);//关闭/开启本地音频
	}

	@Override
	public void switchMediaStream() {
		if (FunStreamType.STREAM_MAIN == mDBind.funVideoView.getStreamType()) {
			mDBind.funVideoView.setStreamType(FunStreamType.STREAM_SECONDARY);
		} else {
			mDBind.funVideoView.setStreamType(FunStreamType.STREAM_MAIN);
		}
		mDBind.funVideoView.stopPlayback();
		playRealMedia();
	}

	@Override
	public void hideDirection(){
		if(mDBind.leftDirection.getVisibility() == View.VISIBLE){
			mDBind.leftDirection.setVisibility(View.GONE);
		}
		if(mDBind.rightDirection.getVisibility() == View.VISIBLE){
			mDBind.rightDirection.setVisibility(View.GONE);
		}
		if(mDBind.downDirection.getVisibility() == View.VISIBLE){
			mDBind.downDirection.setVisibility(View.GONE);
		}
		if(mDBind.upDirection.getVisibility() == View.VISIBLE){
			mDBind.upDirection.setVisibility(View.GONE);
		}
		mHandler.removeMessages(MESSAGE_AUTO_HIDE_DIRECTION);
	}

	@Override
	public void onLoginFailed() {
		// 设备登录失败
		dismissProgressDialog();
		mDBind.textVideoStat.setVisibility(View.GONE);
	}

	@Override
	public void showVoice(boolean play) {
		mDBind.btnVoice.setImageResource(play ? R.mipmap.icon_is_voice : R.mipmap.icon_no_voice);
	}

	@Override
	public void showToastMsg(String msg) {
		showToast(msg);
	}

	@Override
	public void stopMedia() {
		mDBind.funVideoView.stopPlayback();
		mDBind.funVideoView.stopRecordVideo();
	}

	@Override
	public void startActivityByIntent(Intent intent) {
		startActivity(intent);
	}

	@Override
	public void finishActivity() {
		finish();
	}

	private Handler mHandler = new Handler(msg -> {
		if (msg.what == MESSAGE_AUTO_HIDE_DIRECTION) {
			hideDirection();
		}
		return false;
	});


	private class OnVideoViewTouchListener implements OnTouchListener {

		private float x1;
		private float y1;
		private final static int HandMoveSize = 12;

		@SuppressLint("ClickableViewAccessibility")
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.e(TAG, "MotionEvent Action: " + event.getAction());
			switch (event.getAction()){
				case MotionEvent.ACTION_DOWN:
					x1 = event.getX();
					y1 = event.getY();
					break;
				case MotionEvent.ACTION_UP:
					mPresent.onContractPTZ1(nPTZCommand, true);
					// 显示或隐藏视频操作菜单
					if (mDBind.layoutVideoControl.getVisibility() == View.VISIBLE) {
						hideVideoControlBar();
					} else {
						showVideoControlBar();
					}
					break;
				case MotionEvent.ACTION_MOVE:
					float x2 = event.getX();
					float y2 = event.getY();
					if (x2 - x1 > HandMoveSize) {
						showSlideAction(ActionType.ACTION_RIGHT); // right
					} else if (x1 - x2 > HandMoveSize) {
						showSlideAction(ActionType.ACTION_LEFT); // left
					} else if (y2 - y1 > HandMoveSize) {
						showSlideAction(ActionType.ACTION_DOWN); // down
					} else if (y1 - y2 > HandMoveSize) {
						showSlideAction(ActionType.ACTION_UP); // up
					}
					break;
			}
			return true;
		}
	}


	private void showSlideAction(ActionType type){
		switch (type){
			case ACTION_DOWN:
				nPTZCommand = EPTZCMD.TILT_UP;
				break;
			case ACTION_UP:
				nPTZCommand = EPTZCMD.TILT_DOWN;
				break;
			case ACTION_RIGHT:
				nPTZCommand = EPTZCMD.PAN_LEFT;
				break;
			case ACTION_LEFT:
				nPTZCommand = EPTZCMD.PAN_RIGHT;
				break;
		}
		mDBind.rightDirection.setVisibility(type == ActionType.ACTION_RIGHT ? View.VISIBLE : View.GONE);
		mDBind.leftDirection.setVisibility(type == ActionType.ACTION_LEFT ? View.VISIBLE : View.GONE);
		mDBind.upDirection.setVisibility(type == ActionType.ACTION_UP ? View.VISIBLE : View.GONE);
		mDBind.downDirection.setVisibility(type == ActionType.ACTION_DOWN ? View.VISIBLE : View.GONE);
		mHandler.sendEmptyMessageDelayed(MESSAGE_AUTO_HIDE_DIRECTION, 2000);
		mPresent.onContractPTZ1(nPTZCommand, false);
	}

	private void showVideoControlBar() {
		if (mDBind.layoutVideoControl.getVisibility() != View.VISIBLE) {
			TranslateAnimation animation = new TranslateAnimation(0, 0, UIFactory.dip2px(this, 40), 0);
			animation.setDuration(500);
			mDBind.layoutVideoControl.startAnimation(animation);
			mDBind.layoutVideoControl.setVisibility(View.VISIBLE);
		}

		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// 横屏情况下,顶部标题栏也动画显示
			TranslateAnimation animation = new TranslateAnimation(0, 0, -UIFactory.dip2px(this, 50), 0);
			animation.setDuration(500);
			mDBind.layoutCameraTop.startAnimation(animation);
			mDBind.layoutCameraTop.setVisibility(View.VISIBLE);
			mPresent.showVideoControlBar();
		} else {
			mDBind.layoutCameraTop.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void hideVideoControlBar() {
		// 横屏情况下,顶部标题栏也隐藏
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			if (mDBind.layoutVideoControl.getVisibility() != View.GONE) {
				TranslateAnimation ani = new TranslateAnimation(0, 0, 0, UIFactory.dip2px(this, 40));
				ani.setDuration(200);
				mDBind.layoutVideoControl.startAnimation(ani);
				mDBind.layoutVideoControl.setVisibility(View.GONE);
			}
			TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -UIFactory.dip2px(this, 50));
			animation.setDuration(200);
			mDBind.layoutCameraTop.startAnimation(animation);
			mDBind.layoutCameraTop.setVisibility(View.GONE);
			mPresent.hideVideoControlBar();
		}
	}

	@SuppressLint("ClickableViewAccessibility")
	private OnTouchListener mIntercomTouchLs = (arg0, arg1) -> {
		try {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				mPresent.startTalk();
			} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
				mPresent.stopTalk();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	};

	@Override
	public void onBackPressed() {
		mPresent.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		stopMedia();
		FunSupport.getInstance().removeOnFunDeviceOptListener(this);
		FunSupport.getInstance().removeOnFunDeviceListener(this);
		if (null != mHandler) {
			mHandler.removeCallbacksAndMessages(null);
			mHandler = null;
		}
		if (mMediaPlayer!=null){
			mMediaPlayer.destroy();
		}
		super.onDestroy();
	}
}
